package br.com.dev.tcc.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.location.Location;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.DelegateListener;
import br.com.dev.tcc.bean.ResponseData;
import br.com.dev.tcc.singleton.LocationSingleton;
import br.com.dev.tcc.task.BuscaLocaisProximosTask;
import br.com.dev.tcc.view.dialog.ErrorDialog;
import br.com.dev.tcc.view.dialog.ProgressCustomDialog;
import br.com.dev.tcc.view.dialog.SettingsDialog;

import com.google.android.gms.maps.model.LatLng;

public class EnderecoUtil {

	private ProgressCustomDialog progressDialog;
	private boolean cancel;
	private int requests = 0;

	public void searchEndereco(final Context context, final EditText editText) {

		final Handler handler = new Handler();

		if (isGpsEnabled(context)) {

			if (progressDialog == null) {

				progressDialog = new ProgressCustomDialog(context);
				progressDialog.setMessage(R.string.obtendo_local);
				progressDialog.setCancelable(true);
				progressDialog.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						cancel = true;
						progressDialog.dismiss();
					}
				});
				progressDialog.show();
			}

			Location location = LocationSingleton.getInstance().getLocation();

			if (location != null && !cancel) {

				LatLng latLng = new LatLng(location.getLatitude(),
						location.getLongitude());

				String message = context.getString(R.string.buscando_endereco);
				String accuracy = Float.toString(location.getAccuracy());
				if (accuracy.contains("."))
					accuracy = accuracy.substring(0, accuracy.indexOf("."));
				message = message.replace("?", accuracy);
				progressDialog.setMessage(message);

				new BuscaLocaisProximosTask(context, latLng,
						new DelegateListener<ResponseData<String>>() {

							@Override
							public void onDelegate(ResponseData<String> response) {

								if (!cancel)
									if (response.isSuccess()) {
										progressDialog.dismiss();
										editText.setText(response.getData());
									} else {
										new ErrorDialog(context, response
												.getErrorMessage()).show();
										progressDialog.dismiss();
									}
							}
						}).execute();
			} else {

				if (!cancel) {
					Runnable task = new Runnable() {
						@Override
						public void run() {
							if (!cancel) {
								++requests;
								if (requests < 3)
									searchEndereco(context, editText);
								else {
									requests = 0;
									handler.post(new Runnable() {
										@Override
										public void run() {
											new ErrorDialog(
													context,
													R.string.erro_obter_localizacao)
													.show();
											progressDialog.dismiss();
										}
									});
								}
							}
						}
					};
					if (!cancel)
						handler.postDelayed(task, 5000);
				}
			}
		} else {

			final SettingsDialog gpsDialog = new SettingsDialog(context);
			gpsDialog.setCancelable(false);
			gpsDialog.setMessage(R.string.sem_gps_dialog);

			gpsDialog.setPositiveButtonListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					context.startActivity(GpsUtil.getGpsSettingsIntent());
					gpsDialog.dismiss();
				}
			});

			gpsDialog.setNegativeButtonListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					gpsDialog.dismiss();
				}
			});

			gpsDialog.show();
		}
	}

	private boolean isGpsEnabled(Context context) {

		GpsUtil util = new GpsUtil(context);
		return (util.isGpsEnabled() || util.isGpsNetEnabled());
	}
}